package com.ego.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.admin.dao.SysAdminRoleDao;
import com.ego.admin.po.SysAdminRole;
import com.ego.admin.service.SysAdminRoleService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @program: ego
 * @description: 管理员与角色对应关系
 * @author: KamTang
 * @create: 2022-11-13 09:34
 **/
@Service("userRoleService")
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleDao, SysAdminRole> implements SysAdminRoleService {
    /**
     * 添加or修改
     *
     * @param userId 用户ID
     * @param roleIdList 角色ID列表
     */
    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        // 删除用户与角色的关系，避免重复
        this.removeByIds(Collections.singleton(userId));
        if(roleIdList == null || roleIdList.size() == 0){
            return ;
        }
        // 保存管理员与角色的关系
        for (Long roleId: roleIdList) {
            SysAdminRole adminRole = new SysAdminRole();
            adminRole.setUserId(userId);
            adminRole.setRoleId(roleId);
            this.save(adminRole);
        }
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }

    @Override
    public List<String> queryAllPermsByUserId(Long userId) {
        return baseMapper.queryAllPermsByUserId(userId);
    }
}
